package accountinfo.dao;

import hackathon1212.EMF;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import merchant.dao.Merchant;
import merchant.dao.MerchantDAO;

import accountinfo.dao.Transaction;

public class TransactionsDAO {
	private static Random random = new Random();
	private static DecimalFormat df = new DecimalFormat("#.##");
	private static SimpleDateFormat datef =  new SimpleDateFormat("yyyy-MM-dd");
	/*static {
		createTransactions();
	}*/

	public static void createTransactions() {
				
			save(new Transaction("FC092797284000010007251","DEBIT","BILLED","2009-11-02","374100146223006",	"CHOCCYWOCCYDOODAH","2009-10-06","2009-10-06","484.84", "CHOCOLATE"));
			save(new Transaction("FC092797284000010007252","DEBIT","BILLED","2009-11-02","374100146223006",	"MONTEZUMA'S","2009-10-06",	"2009-10-06","198.99", "CHOCOLATE"));
			save(new Transaction("FC092797284000010007253","DEBIT","BILLED","2009-11-02","374100146223006",	"HOTEL CHOCOLAT BRIGHTON","2009-10-06",	"2009-10-06","198.99", "CHOCOLATE"));
			save(new Transaction("10000000010102999890650","DEBIT","BILLED","2012-01-02","374692067151003", "LATE PAYMENT FEE","2012-01-02", "2012-01-02","12", "LPC"));
			save(new Transaction("10000000010102999890660","DEBIT","BILLED","2012-01-02","374692067151003", "INTEREST CHARGE ","2012-01-02", "2012-01-02","2.97", "INTEREST"));
	
			save(new Transaction("AT112640003000010121578","CREDIT","BILLED","2011-10-16","376701001541001", "PAYMENT RECEIVED - THANK YOU","2011-09-21", "2011-09-20","8342.52", "PAYMENT"));
			save(new Transaction("AT112700003000010054926","DEBIT","BILLED","2011-10-16","376701001541001", "THE DUKE OF YORK PICTUREHOUSE","2011-09-27", "2011-09-26","2957.72", "ENTERTAINMENT"));
			save(new Transaction("AT112750005000010081974","DEBIT","BILLED","2011-10-16","376701001541001", "BRIGHTON PAVILLION","2011-10-02", "2011-10-02","1048.08", "ENTERTAINMENT"));
			save(new Transaction("AT112750005000010081975","DEBIT","BILLED","2011-10-16","376701001541001", "THE BRIGHTON WHEEL","2011-10-02", "2011-10-02","3513.06", "ENTERTAINMENT"));
			save(new Transaction("10000000011016999611450","MEMO","BILLED","2011-10-16","376701001541001", "INTERNET VIDEOS","2011-10-16", "2011-10-16","985.9", "ENTERTAINMENT"));
	
			save(new Transaction("AT112540006000010004747","DEBIT","BILLED","2011-10-05","373280269102007", "TAJ BRIGHTON","2011-09-11", "2011-09-09","79.18", "FOOD"));
			save(new Transaction("AT112540006000010012887","CREDIT","BILLED","2011-10-05","373280269102007", "BILL'S BRIGHTON","2011-09-11", "2011-09-10","10.16", "FOOD"));
			save(new Transaction("AT112700006000010007303","CREDIT","BILLED","2011-10-05","373280269102007", "PAYMENT RECEIVED - THANK YOU","2011-09-27", "2011-09-27","100.00", "FOOD"));
	
			save(new Transaction("AT113110047000010010432","DEBIT","BILLED","2011-12-15","375380000742026", "SYDNEY STREET CYCLES","2011-11-07", "2011-11-06","700.00", "BICYCLES"));
			save(new Transaction("AT113110047000010010433 ","DEBIT","BILLED","2011-12-15","375380000744006", "FREEDOM BIKES","2011-11-07", "2011-11-06","286.00", "BICYCLES"));
	
			save(new Transaction("10000000010930999807690","CREDIT","BILLED","2011-10-27","374660001931004", "THE HAND IN HAND PUB BRIGHTON","2011-10-05", "2011-10-05","321.93", "BEER"));
			save(new Transaction("AT112910011000010003180","DEBIT","BILLED","2011-10-27","374660001931004", "CHARLES STREET BRIGHTON","2011-10-18", "2011-10-17","66.25", "BEER"));
			save(new Transaction("AT112930008000010002948","DEBIT","BILLED","2011-10-27","374660001931004", "THE BEE'S MOUTH BAR BRIGHTON","2011-10-20", "2011-10-19","62.98", "BEER"));
	
			save(new Transaction("AT112540006000010004747","DEBIT","BILLED","2011-10-05","3760600002302007", "SPECS OPTICIAN","2011-09-11", "2011-09-09","79.18", "OPTICAL"));
			save(new Transaction("AT112540006000010012887","CREDIT","BILLED","2011-10-05","3760600002302007", "BROMPTON'S OF BRIGHTON OPTICIAN","2011-09-11", "2011-09-10","10.16", "OPTICAL"));
			save(new Transaction("AT112700006000010007303","CREDIT","BILLED","2011-10-05","3760600002302007", "PAYMENT RECEIVED - THANK YOU","2011-09-27", "2011-09-27","100.00", "PAYMENT"));
			
			save(new Transaction("AT112540006000010004747","DEBIT","BILLED","2011-10-05","3769303093602007", "ROUNDER RECORDS","2011-09-11", "2011-09-09","79.18", "MUSIC"));
			save(new Transaction("AT112540006000010012887","CREDIT","BILLED","2011-10-05","3769303093602007", "RESIDENT MUSIC","2011-09-11", "2011-09-10","10.16", "MUSIC"));
			save(new Transaction("AT112700006000010007303","CREDIT","BILLED","2011-10-05","3769303093602007", "PAYMENT RECEIVED - THANK YOU","2011-09-27", "2011-09-27","100.00", "PAYMENT"));
			
			save(new Transaction("AT112540006000010004747","DEBIT","BILLED","2011-10-05","3749880369402007", "THE BRIGHTON COFFEE COMPANY","2011-09-11", "2011-09-09","79.18", "COFFEE"));
			save(new Transaction("AT112540006000010012887","CREDIT","BILLED","2011-10-05","3749880369402007", "GROUND CAFE","2011-09-11", "2011-09-10","10.16", "COFFEE"));
			save(new Transaction("AT112700006000010007303","CREDIT","BILLED","2011-10-05","3749880369402007", "PAYMENT RECEIVED - THANK YOU","2011-09-27", "2011-09-27","100.00", "PAYMENT"));
			
			save(new Transaction("AT112540006000010004747","DEBIT","BILLED","2011-10-05","3749880369402007", "TAJ BRIGHTON","2011-09-11", "2011-09-09","79.18", "FOOD"));
			save(new Transaction("AT112540006000010012887","CREDIT","BILLED","2011-10-05","3749880369402007", "BILL'S RESTAURANT BRIGHTON","2011-09-11", "2011-09-10","10.16", "FOOD"));
			save(new Transaction("AT112700006000010007303","CREDIT","BILLED","2011-10-05","3749880369402007", "PAYMENT RECEIVED - THANK YOU","2011-09-27", "2011-09-27","100.00", "FOOD"));
	
			save(new Transaction("AT112640003000010121578","CREDIT","BILLED","2011-10-16","3742661467741001", "PAYMENT RECEIVED - THANK YOU","2011-09-21", "2011-09-20","8342.52","PAYMENT"));
			save(new Transaction("AT112700003000010054926","DEBIT","BILLED","2011-10-16","3742661467741001", "THE MAD HATTER CAFE","2011-09-27", "2011-09-26","2957.72", "CAFE"));
			save(new Transaction("AT112750005000010081974","DEBIT","BILLED","2011-10-16","3742661467741001", "RESIDENT MUSIC","2011-10-02", "2011-10-02","1048.08", "MUSIC"));
			save(new Transaction("AT112750005000010081975","DEBIT","BILLED","2011-10-16","3742661467741001", "CITY BOOKSHOP HOVE","2011-10-02", "2011-10-02","3513.06", "BOOKS"));
			save(new Transaction("10000000011016999611450","MEMO","BILLED","2011-10-16","3742661467741001", "INTERNET VIDEOS","2011-10-16", "2011-10-16","985.9", "ENTERTAINMENT"));

	}

	public static List<Transaction> getTransactionDetails(String cardNO) {
		final EntityManager em = EMF.get().createEntityManager();
		try{
			final Query q = em.createQuery("select transaction from Transaction transaction where " +
											"transaction.cardNO = :CARDNO ");
			q.setParameter("CARDNO", cardNO);
			return q.getResultList();
		} finally {
			em.close();
		}
	}
	
	public static boolean save(final Transaction t) {
		final EntityManager em = EMF.get().createEntityManager();
		try{
			final Query q = em.createQuery("select transaction from Transaction transaction where " +
											"transaction.referenceNO = :REFNO ");
			q.setParameter("REFNO", t.getReferenceNO());
			if (q.getResultList().isEmpty()){
				em.merge(t);
				return true;
			} else {
				return false;
			}
		} finally {
			em.close();
		}
	}

	public static Transaction randomTransaction() {
		
		final EntityManager em = EMF.get().createEntityManager();
		Merchant m;
		try{
			final List<Merchant> ms = MerchantDAO.get().getAllMerchants();
			m = ms.get(random.nextInt(ms.size()));		
		} finally {
			em.close();
		}
		
		final Date now = new Date();
		final LinkedList<String> ads = new LinkedList<String>();
		
		ads.addAll(AccountDetailsDAO.cardAccounts.keySet());
		final ArrayList<AccountDetail> alads = AccountDetailsDAO.cardAccounts.get(ads.get(random.nextInt(ads.size())));		
		
		return new Transaction(
				String.format("%023d", Math.abs(random.nextLong())),
				random.nextBoolean() ? "CREDIT" : "DEBIT",
				random.nextBoolean() ? "BILLED" : "UNBILLED",
				datef.format(now),
				alads.get(random.nextInt(alads.size())).cardNO,
				m.getDoingBusinessAsName(),
				datef.format(now), 
				datef.format(now),
				df.format(Math.abs(random.nextDouble() * 1000)) , 
				m.getIndustryDetail());
		
		
	}
}
